<template>
  <div>
    <van-nav-bar title="注册" left-arrow @click-left="$router.back()" />
<div class="title" style="margin-left:16px;">
  <h2>注册莱头条账号</h2>
</div>
    <van-form @submit="onSubmit" ref="regForm">
      <van-field
        v-model="username"
        name="username"
       
        placeholder="请输入手机号"
        :rules="[
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' },
        ]"
      />

      <van-field
        v-model="vercode"
        name="vercode"
        center
        clearable
       
        placeholder="请输入短信验证码"
        :rules="[
          { required: true, message: '请填写验证码' },
          {
            pattern: /^\d{6}$/,
            message: '验证码格式错误',
          },
        ]"
      >
        <template #button>
          <van-button
            round
            size="small"
            type="primary"
            @click="send"
            native-type="button"
            :disabled="btnDisabled"
            >{{ code_text }}</van-button
          >
        </template>
      </van-field>

      <van-field
        v-model="password"
        type="password"
        name="password"
       
        placeholder="请设置密码"
        :rules="[
          { required: true, message: '请填写密码' },
          {
            validator: (e) => /^\w{6,12}$/.test(e),
            message: '密码格式为6-12位字母或数字',
          },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
       
          :loading="submitLoading"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { reg, sendSms, delete_user } from "@/api/user.js";
export default {
  data() {
    return {
      username: "",
      password: "",
      vercode: "",
      btnDisabled: false,
      code_text: "发送验证码",
      submitLoading: false,
    };
  },
  created() {},
  methods: {
    onSubmit(values) {
      console.log("submit", values);
      this.submitLoading = true;
      reg(values)
        .then((res) => {
          console.log(res);

          // 不管成功还是失败都会进倒下面这个
        })
        .finally((e) => {
          this.submitLoading = false;
        });
    },
    async send() {
      try {
        await this.$refs.regForm.validate("username");
        //    发送验证码按钮点击进入倒计时
        this.btnDisabled = true; //变成不可点击状态
        this.code_text = "发送中...";
        // 定义一个定时器
        let timer = null;
        // 定义一个倒计时的时间
        let count = 10;

        sendSms({
          mobile: this.username,
          type: "register",
        })
          .then((res) => {
            if (res.code == 0) {
              this.$toast(`验证码已经发送到${this.username},请注意查收`);

              timer = setInterval((e) => {
                count--;
                this.code_text = `${count}s后再次发送`;
                if (count <= 0) {
                  clearInterval(timer);
                  count = 10;
                  this.code_text = "再次获取";
                  this.btnDisabled = false;
                }
              }, 1000);
            } else {
              // 例如账号已存在等情况
              this.$toast(res.msg);
              this.btnDisabled = false; //变成不可点击状态
              this.code_text = "再次发送";
            }

            //   例如网络错误等原因进入到下面这个catch  并且提示他发送失败了 并进行再次发送  并且恢复到可点击状态
          })
          .catch((err) => {
            this.$toast(res.msg || "网络错误");
            this.btnDisabled = false; //变成不可点击状态
            this.code_text = "请再次请求";
          });
      } catch (error) {}
    },
  },
};
</script>

<style lang="less" scoped>
::v-deep  .van-button--info{
 background-color: @baseColor;
 border: none;
}
::v-deep  .van-button--primary{
  border: none;
}
</style>